package gframework.bean.field;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

    public class GPlainDocument extends PlainDocument {

    public static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
    public static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String ALPHA = LOWERCASE + UPPERCASE;
    public static final String NUMERIC = "0123456789";
    public static final String FLOAT = NUMERIC + ",.";
    public static final String WHITE_SPACE = " ";
    public static final String OTHER_CHARACTERS = "çÇªº/?!@#$%&*()-_=+[{]}:;.>,<\\|\"ãÃáÁàÀâÂéÉíÍóÓõÕôÔúÚùÙ\n\t\r";
    public static final String ALPHA_NUMERIC = ALPHA + NUMERIC + WHITE_SPACE + OTHER_CHARACTERS;
    public static final String SPECIAL_CHARACTERS = "'";
    public static final String ALL_CHARACTERS = ALPHA_NUMERIC + SPECIAL_CHARACTERS;
    private String acceptedChars = null;
    private boolean caixaAlta = false;
    private int columns = 0;
    private int casaDecimal = 0;
    private boolean negativo = false;

    public GPlainDocument() {
        this.acceptedChars = ALPHA_NUMERIC;
        this.caixaAlta = true;
    }

    public void setAcceptedChars(String i_acceptedChars) {
        acceptedChars = i_acceptedChars;
    }

    @Override
    public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
        if ((str == null) || (str.equals(""))) {
            return;
        }

        if (isCaixaAlta()) {
            str = str.toUpperCase();
        }

        for (int i = 0; i < str.length(); i++) {
            if ((acceptedChars.indexOf(String.valueOf(str.charAt(i))) == -1) && !(isNegativo() && str.charAt(i) == '-')) {
                return;
            }
        }

        if (acceptedChars.equals(FLOAT)) {
            if ((getText(0, getLength()).indexOf(".") != -1) || (getText(0, getLength()).indexOf(",") != -1)) {
                if ((str.indexOf(".") != -1) || (str.indexOf(",") != -1)) {
                    return;

                } else {
                    if ((getText(0, getLength()).indexOf(".") != -1) && ((getLength() - (getCasaDecimal() + 1)) == getText(0, getLength()).indexOf("."))) {
                        return;
                    } else if ((getText(0, getLength()).indexOf(",") != -1) && ((getLength() - (getCasaDecimal() + 1)) == getText(0, getLength()).indexOf(","))) {
                        return;
                    }
                }
            }
        }

        if (isNegativo() && str.indexOf("-") != -1) {
            if (str.indexOf("-") != 0 || offset != 0) {
                return;
            }
        }

//        if (isDesconto() && str.indexOf("+") != -1) {
//            if (str.contains("+") || offset != 0) {
//                return;
//            }
//        }

        if ((getColumns() == 0) || (getLength() + str.length() <= getColumns())) {
            super.insertString(offset, str, attr);

        } else {
            str = str.substring(0, (getColumns() - getLength()));
            super.insertString(offset, str, attr);
        }
    }

    public int getColumns() {
        return columns;
    }

    public void setColumns(int i_columns) {
        this.columns = i_columns;
    }

    public boolean isCaixaAlta() {
        return caixaAlta;
    }

    public void setCaixaAlta(boolean i_caixaAlta) {
        this.caixaAlta = i_caixaAlta;
    }

    public int getCasaDecimal() {
        return casaDecimal;
    }

    public void setCasaDecimal(int casaDecimal) {
        this.casaDecimal = casaDecimal;
    }

    public boolean isNegativo() {
        return negativo;
    }

    public void setNegativo(boolean negativo) {
        this.negativo = negativo;
    }

}

